import marimo

__generated_with = "0.9.27"
app = marimo.App(width="medium", app_title="button and switch")


@app.cell
def __():
    import marimo as mo

    switch_ui = mo.ui.switch(label='switch')
    switch_ui
    return mo, switch_ui


@app.cell
def __(mo, switch_ui):


    mo.md(f"**switch开关的状态是`{switch_ui.value}`**")
    return


@app.cell
def __(mo):
    # 按钮弹出提示


    # 函数必须要有参数，不然增加到on_click会报错
    # 如果没有返回值，按下按钮，返回None
    def show_tips(a):
        mo.status.toast(f'你点击了按钮{a}', kind='danger')
        return a+1
    return (show_tips,)


@app.cell
def __(mo, show_tips):

    # on_click运行函数

    button_ui = mo.ui.button(label='按钮', on_click=show_tips, value=123)
    button_ui
    return (button_ui,)


@app.cell
def __(button_ui, mo):
    # button_ui

    mo.md(f"**button按钮的状态是`{button_ui.value}`**")
    return


@app.cell
def __(button_ui):
    from datetime import datetime

    # 增加此句，则按动按钮，与打印日期关联
    button_ui

    print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    return (datetime,)


@app.cell
def __(mo):
    run_butt_ui = mo.ui.run_button(label='执行按钮')
    run_butt_ui
    return (run_butt_ui,)


@app.cell
def __(mo, run_butt_ui):
    mo.md(f'执行按钮的状态是{run_butt_ui.value}')
    return


@app.cell
def __(mo, run_butt_ui):
    # 运行后文字不显示，按下执行按钮才会显示文字


    mo.stop(not run_butt_ui.value)
    mo.md('按钮显示文本')
    return


@app.cell
def __(mo):
    mo.md(r"""button与run_button的区别""")
    return


@app.cell
def __(mo):
    aui = mo.ui.slider(1, 10)
    aui
    return (aui,)


@app.cell
def __(mo):
    # button不影响滑块值的显示
    # run_button按下后，滑块值才更新



    # button
    # butt_1 = mo.ui.button()


    # run_button
    butt_1 = mo.ui.run_button()



    butt_1
    return (butt_1,)


@app.cell
def __(aui, butt_1, mo):

    # button
    # butt_1


    # run_button
    mo.stop(not butt_1.value)

    mo.md(f'滑块 = {aui.value}')
    return


if __name__ == "__main__":
    app.run()
